package com.pkx.guide.system.controller;

import com.pkx.guide.common.annotation.ORpose;
import com.pkx.guide.system.model.form.MenuForm;
import com.pkx.guide.system.model.query.MenuQuery;
import com.pkx.guide.common.annotation.PreventDuplicateSubmit;
import com.pkx.guide.system.model.vo.MenuVO;
import com.pkx.guide.common.model.Option;
import com.pkx.guide.system.model.vo.RouteVO;
import com.pkx.guide.system.service.TSysMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单控制器
 *
 * @author haoxr
 * @since 2020/11/06
 */
@Tag(name = "菜单接口")
@RestController
@RequestMapping("/menus")
@Slf4j
@ORpose
public class TSysMenuController {

    @Resource
    private TSysMenuService menuService;

    @Operation(summary = "菜单列表",security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping
    public List<MenuVO> listMenus( @ParameterObject MenuQuery queryParams) {
        return menuService.listMenus(queryParams);
    }

    @Operation(summary = "菜单下拉列表",security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/options")
    public List<Option> listMenuOptions() {
        return menuService.listMenuOptions();
    }

    @Operation(summary = "路由列表",security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/routes")
    public List<RouteVO> listRoutes() {
        return menuService.listRoutes();
    }

    @Operation(summary = "菜单表单数据",security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/{id}/form")
    public MenuForm getMenuForm(@Parameter(description =  "菜单ID") @PathVariable Long id) {
        return menuService.getMenuForm(id);
    }

    @Operation(summary = "新增菜单",security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:menu:add')")
    @PreventDuplicateSubmit
    @CacheEvict(cacheNames = "system", key = "'routes'")
    public Boolean addMenu(@RequestBody MenuForm menuForm) {
        return menuService.saveMenu(menuForm);
    }

    @Operation(summary = "修改菜单",security = {@SecurityRequirement(name = "Authorization")})
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('sys:menu:edit')")
    @CacheEvict(cacheNames = "system", key = "'routes'")
    public Boolean updateMenu(@RequestBody MenuForm menuForm) {
        return menuService.saveMenu(menuForm);
    }

    @Operation(summary = "删除菜单",security = {@SecurityRequirement(name = "Authorization")})
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPerm('sys:menu:delete')")
    @CacheEvict(cacheNames = "system", key = "'routes'")
    public Boolean deleteMenu(@Parameter(description ="菜单ID，多个以英文(,)分割") @PathVariable("id") Long id) {
        return menuService.deleteMenu(id);
    }

    @Operation(summary = "修改菜单显示状态",security = {@SecurityRequirement(name = "Authorization")})
    @PatchMapping("/{menuId}")
    public Boolean updateMenuVisible(@Parameter(description =  "菜单ID") @PathVariable Long menuId, @Parameter(description =  "显示状态(1:显示;0:隐藏)") Integer visible) {
        return menuService.updateMenuVisible(menuId, visible);
    }
}

